package org.example.Server.controller.user;

import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;
import org.example.Server.model.user.UserDTO;
import org.example.Server.model.user.UserVO;
import org.example.Server.service.user.UserService;
import org.example.Server.utils.ImageCode;
import org.example.Server.utils.JwtUtils;
import org.example.Server.utils.ResultAPI;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;


import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import static org.example.Server.utils.ExceptionEnum.USER_NOTEXIST;


/**
 * 用户登录
 */
@RestController
@Slf4j
@RequestMapping("/youxich/user")
public class LoginController {
    @Autowired
    UserService userService;

    @Transactional
    @PostMapping("/login")
    public ResultAPI login(@RequestBody UserDTO user) {
//        String imageCode = user.getImageCode();
//        // 获取验证码
//        String checkCode = (String) httpSession.getAttribute(ImageCode.class.getName());
//
//        // 这种情况一般都是直接访问login了
//        if (imageCode.isEmpty()) {
//            return ResultAPI.error("验证码不能为空！");
//        }
//
//        if (checkCode.isEmpty() || !checkCode.equalsIgnoreCase(imageCode)) {
//            return ResultAPI.error("验证码错误！");
//        }
        return userService.login(user);
    }

    @RequestMapping("/getImageCode")
    public void getImageCode(HttpServletRequest request, HttpServletResponse response) {
        try {
            new ImageCode().doGet(request, response);
        } catch (ServletException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
